package com.sfddata.musicpillow.user.service.impl;

import com.sfddata.musicpillow.user.entity.EquipWithPhone;
import com.sfddata.musicpillow.user.entity.UserEquipRelationEntity;
import com.sfddata.musicpillow.user.entity.UserInfoEntity;
import com.sfddata.musicpillow.user.service.UserEquipRelationService;
import com.sfddata.musicpillow.user.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfddata.musicpillow.common.utils.PageUtils;
import com.sfddata.musicpillow.common.utils.Query;
import com.sfddata.musicpillow.user.dao.EquipmentInfoDao;
import com.sfddata.musicpillow.user.entity.EquipmentInfoEntity;
import com.sfddata.musicpillow.user.service.EquipmentInfoService;


@Service("equipmentInfoService")
public class EquipmentInfoServiceImpl extends ServiceImpl<EquipmentInfoDao, EquipmentInfoEntity> implements EquipmentInfoService {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    UserEquipRelationService userEquipRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EquipmentInfoEntity> page = this.page(
                new Query<EquipmentInfoEntity>().getPage(params),
                new QueryWrapper<EquipmentInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<EquipmentInfoEntity> getByUserId(Integer userId) {

        List<EquipmentInfoEntity> equipmentInfoEntities = baseMapper.getEQListByUserId(userId);
        for (EquipmentInfoEntity equipmentInfoEntity : equipmentInfoEntities) {

            
        }
        return equipmentInfoEntities;
    }

    @Override
    public void saveWithUser(EquipWithPhone info) {

        EquipmentInfoEntity equip = new EquipmentInfoEntity();
        BeanUtils.copyProperties(info, equip);
        // 防止设备重复
        EquipmentInfoEntity equipEntity = this.insertNotExit(equip);
        // 获取手机的id
        UserInfoEntity user = userInfoService.getByAccount(info.getPhone());
        // 更新relation信息，防止关联关系重复
        UserEquipRelationEntity relationEntity = new UserEquipRelationEntity();
        relationEntity.setEquipId(equipEntity.getId());
        relationEntity.setUserId(user.getId());
        userEquipRelationService.save(relationEntity);
    }


    @Override
    public EquipmentInfoEntity insertNotExit(EquipmentInfoEntity equip) {
        QueryWrapper<EquipmentInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BleName", equip.getBlename());
        EquipmentInfoEntity entity = baseMapper.selectOne(queryWrapper);
        if (entity == null){
            // 新设备
            baseMapper.insert(equip);
            return entity;
        }else {
            // 老设备
            return entity;
        }
    }


    @Override
    public void removeRelation(EquipWithPhone info) {
        // 根据设备信息 获取relation
        String phone = info.getPhone();
        String ble = info.getBlename();

        QueryWrapper<EquipmentInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BleName", ble);
        EquipmentInfoEntity equip = baseMapper.selectOne(queryWrapper);
        UserInfoEntity user = userInfoService.getByAccount(phone);

        userEquipRelationService.removeByEquipAndUser(equip.getId(), user.getId());
    }


}